/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formJenisFilm.java
 *
 * Created on Oct 12, 2011, 5:55:16 PM
 */
package com.system.formgui.source;

import com.system.source.model.JenisFilmTabelModel;
import com.system.source.connector.DataBaseConnection;
import com.system.source.data.jenisFilm;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Pinds
 */
public class formJenisFilm extends javax.swing.JInternalFrame {

    /** Creates new form formJenisFilm */
    public formJenisFilm() {
        this.initComponents();
        this.setTitle("Jenis Film");
        this.tabelModel = new JenisFilmTabelModel();
        this.tbJenisFilm.setModel(tabelModel);
        this.fillTabel();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        lKode = new javax.swing.JLabel();
        tKode = new javax.swing.JTextField();
        lJenisFilm = new javax.swing.JLabel();
        tJenisFilm = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbJenisFilm = new javax.swing.JTable();
        bTutup = new javax.swing.JButton();
        bTambah = new javax.swing.JButton();
        bUbah = new javax.swing.JButton();

        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/com/system/formgui/images/film_add.png"))); // NOI18N

        jPanel2.setBackground(new java.awt.Color(204, 255, 204));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 44, Short.MAX_VALUE)
        );

        lKode.setText("Kode Jenis Film");

        lJenisFilm.setText("Jenis Film");

        tbJenisFilm.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Kode", "Jenis Film"
            }
        ));
        tbJenisFilm.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbJenisFilmMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbJenisFilm);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lKode)
                            .addComponent(lJenisFilm))
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tKode, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tJenisFilm, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lKode)
                    .addComponent(tKode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lJenisFilm)
                    .addComponent(tJenisFilm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bTutup.setText("Tutup");
        bTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bTutupActionPerformed(evt);
            }
        });

        bTambah.setText("Tambah");
        bTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bTambahActionPerformed(evt);
            }
        });

        bUbah.setText("Ubah");
        bUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUbahActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(136, 136, 136)
                .addComponent(bUbah, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bTambah)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bTutup, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bTutup)
                    .addComponent(bTambah)
                    .addComponent(bUbah))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void fillTabel() {
        try {
            int row = tbJenisFilm.getRowCount();
            for (int i = 0; i < row; i++) {
                tabelModel.hapusTabel(0, row);
            }
            Statement st = DataBaseConnection.getConnection().createStatement();
            String sql = "select * from jenisfilm order by Kode_JenisFilm";
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                jenisFilm jF = new jenisFilm();
                jF.setKode_JenisFilm(rs.getInt(1));
                jF.setJenisFilm(rs.getString(2));
                tabelModel.tambahJenisFilm(jF);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
private void bTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bTutupActionPerformed
    this.setVisible(false);
}//GEN-LAST:event_bTutupActionPerformed

private void bTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bTambahActionPerformed
    if ((tKode.getText().equals("")) || (tJenisFilm.getText().equals(""))) {
        JOptionPane.showMessageDialog(null, "Data Harus diisi lengkap...!");
    } else {
        try {
            Statement st = DataBaseConnection.getConnection().createStatement();
            String sql = "insert into jenisfilm values(" + tKode.getText() + ",'" + tJenisFilm.getText() + "')";
            st.execute(sql);
            fillTabel();
            tKode.setText("");
            tJenisFilm.setText("");
            JOptionPane.showMessageDialog(null, "Data berhasil ditambah");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}//GEN-LAST:event_bTambahActionPerformed

private void tbJenisFilmMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbJenisFilmMouseClicked
    if (tbJenisFilm.isEnabled()) {
        int n = 0;
        n = tbJenisFilm.getSelectedRow();
        tKode.setText(tbJenisFilm.getValueAt(n, 0).toString());
        tJenisFilm.setText(tbJenisFilm.getValueAt(n, 1).toString());
    }
}//GEN-LAST:event_tbJenisFilmMouseClicked

private void bUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUbahActionPerformed
    try {
        Statement st = DataBaseConnection.getConnection().createStatement();
        String sql = "Update jenisfilm SET "
                + "Kode_JenisFilm=" + tKode.getText() + ","
                + "JenisFilm='" + tJenisFilm.getText() + "' "
                + "where Kode_JenisFilm = '" + tKode.getText() + "'";
        st.execute(sql);
        fillTabel();
        tKode.setText("");
        tJenisFilm.setText("");
        JOptionPane.showMessageDialog(null, "Data berhasil diubah");
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_bUbahActionPerformed
    private JenisFilmTabelModel tabelModel;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bTambah;
    private javax.swing.JButton bTutup;
    private javax.swing.JButton bUbah;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lJenisFilm;
    private javax.swing.JLabel lKode;
    private javax.swing.JTextField tJenisFilm;
    private javax.swing.JTextField tKode;
    private javax.swing.JTable tbJenisFilm;
    // End of variables declaration//GEN-END:variables
}
